import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../views/Login'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../views/Login')
// import Home from '../views/Home'
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../views/Home')

// import Welcome from '../components/Welcome'
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

// Users
// import Users from '../components/users/Users'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/users/Users.vue')


// power
// import Roles from '../components/power/Roles'
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')
// import Rights from '../components/power/Rights'
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')


// goods
// import Add from '@/components/goods/Add'
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')
// import Categories from '@/components/goods/Categories'
const Categories = () => import(/* webpackChunkName: "Categories_Params" */ '../components/goods/Categories.vue')
// import List from '@/components/goods/List'
const List = () => import(/* webpackChunkName: "List_Add" */ '../components/goods/List.vue')
// import Params from '@/components/goods/Params'
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')


// order
// import Orders from '@/components/order/Orders'
const Orders = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Orders.vue')

// report
// import Reports from '@/components/report/Reports'
const Reports = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Reports.vue')


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/categories',
        component: Categories
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/orders',
        component: Orders
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  }

]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
// 当我把token删掉以后再进入 home 页面就会调转到 login 页面 因为没有 token 不合法
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径跳转而来
  // next 是一个函数 表示放行
  // next() 放行  next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()


})

export default router
